<?php

namespace Home\Controller;

/**
 * 客栈组管理接口
 * @modify trainyao 2016-07-26  创建
 */
class GroupController extends \Home\Controller\ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 保存组信息
     * @modify trainyao 2016-07-26    创建
     */
    public function save() {
        $param = I('post.');
        $groupInfoArray = isset($param['group']) ? $param['group'] : array();
        if (!empty($groupInfoArray)) {
            $validateRule = array(
                'id' => 'notnull,notempty,natureNumber',
                'name' => 'notnull,notempty,nameLength15',
            );
            foreach ($groupInfoArray as $groupInfoArrayItem) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $groupInfoArrayItem, $validateRule);
                if (empty($validatePass)) {
                    return;
                }
            }
            unset($groupInfoArrayItem);
        }

        // 校验是否数目大于5
        if (count($groupInfoArray) > 5) {
            returnJson(403, 202);
            return;
        }
        // 取出传入ID,校验是否可以操作
        $userId = getUserId();
        $groupLogic = A('Common/Group', 'Logic');
        $groupId = array_unique(array_column($groupInfoArray, 'id'));
        // 去除新建组传入的0id
        $groupId = array_diff($groupId, array(0));
        if (!$groupLogic->check($userId, $groupId)) {
            returnJson(403, 202);
            return;
        }

        // 保存组信息
        try {
            $groupLogic->saveInfo($groupInfoArray, $userId);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 保存组-子管理员关系
     * @modify trainyao 2016-07-26    创建
     */
    public function saveAccount() {
        $param = I('post.');
        $validateRule = array(
            'groupId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $inGroupAccountId = isset($param['inGroupAccountId']) ? $param['inGroupAccountId'] : array();
        if (!is_array($inGroupAccountId)) {
            returnJson(403, 213);
            return;
        }
        if (!empty($inGroupAccountId)) {
            foreach ($inGroupAccountId as $accountId) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $accountId, 'id');
                if (empty($validatePass)) {
                    return;
                }
            }
            unset($accountId);
        }
        $notInGroupAccountId = isset($param['notInGroupAccountId']) ? $param['notInGroupAccountId'] : array();
        if (!is_array($notInGroupAccountId)) {
            returnJson(403, 213);
            return;
        }
        if (!empty($notInGroupAccountId)) {
            foreach ($notInGroupAccountId as $accountId) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $accountId, 'id');
                if (empty($validatePass)) {
                    return;
                }
            }
            unset($accountId);
        }

        // 校验在组与不在组的子管理员ID是否有重复
        if (!empty(array_intersect($inGroupAccountId, $notInGroupAccountId))) {
            returnJson(403, 202);
            return;
        }

        // 校验是否有权限操作组
        $userId = getUserId();
        $groupId = $param['groupId'];
        $groupLogic = A('Common/Group', 'Logic');
        $canOperate = $groupLogic->check($userId, $groupId);
        if (empty($canOperate)) {
            returnJson(403, 202);
            return;
        }

        // 是否是超级管理员的子管理员
        $accountLogic = A('Common/Account', 'Logic');
        $canOperate = $accountLogic->checkCanOperate($userId, array_merge($inGroupAccountId, $notInGroupAccountId));
        if (empty($canOperate)) {
            returnJson(403, 202);
            return;
        }

        // 校验传入的子管理员ID是否已经有组
        try {
            $groupLogic->checkCanAddInGroup($groupId, $inGroupAccountId);
        } catch (\Exception $e) {
            $message = explode(',', $e->getMessage());
            if ($message[1] == 210) {
                returnJson($message[0], $message[1], array(
                    'userId' => $e->getCode(),
                ));
                return;
            }

            returnJson($message);
            return;
        }

        // 保存关系
        try {
            $groupLogic->saveAccount($groupId, $inGroupAccountId, $notInGroupAccountId);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }
}
